﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.Maintenance;
using WaterRevenueMS.Core.Repository.Charge;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.Charge;
using System.Linq.Expressions;
using WaterRevenueMS.Domain.ViewModels.Customer.Customer;
using WaterRevenueMS.Core.Repository.Customer;

namespace WaterRevenueMS.Core.Service.Charge.Impl
{

    [Export(typeof(IChargeRecordService))]
    public class ChargeRecordService : CoreServiceBase, IChargeRecordService
    {
        [Import]
        public IChargeRecordRepository ChargeRecordRepository { get; set; }

        [Import]
        public WaterRevenueMS.Core.Repository.Customer.ICustomerRepository CustomerRepository { get; set; }



        public IQueryable<Componnent.Entities.Charge.ChargeRecord> ChargeRecords
        {
            get { return ChargeRecordRepository.Entities; }
        }

        public IQueryable<WaterRevenueMS.Componnent.Entities.Customer.Customer> Customers
        {
            get { return CustomerRepository.Entities; }
        }

        public OperationResult Insert(Domain.ViewModels.Charge.ChargeRecord.ChargeRecordModel model)
        {
            var entity = new ChargeRecord
            {
                Ben_CJF=model.Ben_CJF,
                Bei_Z = model.Bei_Z,
                Ben_QDS = model.Ben_QDS,
                Ben_QYL = model.Ben_QYL,
                User_Id = model.User_ID,
                Jiao_FLX = model.Jiao_FLX,
                Jiao_FZT =1,
                Shang_QDS = model.Shang_QDS,
                CreateDT = DateTime.Now,
                He_JYL = model.He_JYL

            };
            ChargeRecordRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "操作成功");
        }

        public void GetLiuSModel(Domain.ViewModels.Charge.ChargeRecord.ChargeRecordModel model)
        {
            
            var strDt = DateTime.Now.Date.ToString("yyyyMMdd");

            model.Liu_SH = ChargeRecordRepository.Entities.Where(t => t.Liu_SH.Contains(strDt)).Max(t => t.Liu_SH);
            if ((model.Liu_SH == "0") || (model.Liu_SH == null))
            {
                model.Liu_SH = strDt + "0001";
            }
            else
            {
                Int64 ints = Int64.Parse(model.Liu_SH);
                ints = ints + 1;
                model.Liu_SH = ints.ToString();
            }
        }

        public void GetLiuSModel(Domain.ViewModels.Charge.MeterCharge.MeterChargeModel model)
        {
            
            var strDt = DateTime.Now.Date.ToString("yyyyMMdd");

            model.Liu_SH = ChargeRecordRepository.Entities.Where(t => t.Liu_SH.Contains(strDt)).Max(t => t.Liu_SH);
            if ((model.Liu_SH == "0") || (model.Liu_SH == null))
            {
                model.Liu_SH = strDt + "0001";
            }
            else
            {
                Int64 ints = Int64.Parse(model.Liu_SH);
                ints = ints + 1;
                model.Liu_SH = ints.ToString();
            }
        }
        

        public void ChangeState(Domain.ViewModels.Charge.ChargeRecord.ChargeRecordModel model)
        {

            var entity = ChargeRecords.FirstOrDefault(t => t.Id == model.Id);
            {

                entity.Jiao_FZT = model.Jiao_FZT;
                
            };

            ChargeRecordRepository.Update(entity);
          
        }

        public void SaveOneCharge(Domain.ViewModels.Charge.ChargeRecord.ChargeRecordModel model)
        {

            var entity = ChargeRecords.FirstOrDefault(t => t.Id == model.Id);
            {

                entity.Jiao_FZT = model.Jiao_FZT;

            };

            ChargeRecordRepository.Update(entity);
        }


        public void ChangeYE(Domain.ViewModels.Charge.ChargeRecord.ChargeRecordModel model)//更新余额
        {
            

            var entity = Customers.FirstOrDefault(t => t.Id == model.User_ID);
            {
                model.Up_YE = entity.Zhang_HYE + model.Jiao_FJE;
                entity.Zhang_HYE = model.Up_YE;

                var entityw = ChargeRecords.FirstOrDefault(t => t.Id == model.Id);
                {

                    entityw.Jiao_FJE = model.Jiao_FJE;

                };

                ChargeRecordRepository.Update(entityw);
            };
            CustomerRepository.Update(entity);
         //   return 1;
  
        }

        public void CheXYE(Domain.ViewModels.Charge.ChargeRecord.ChargeRecordModel model)//撤销收费记录
        {
            

            var entity = Customers.FirstOrDefault(t => t.Id == model.User_ID);
            {
                model.Up_YE = entity.Zhang_HYE - model.Jiao_FJE;
                entity.Zhang_HYE = model.Up_YE;
           
            };
            CustomerRepository.Update(entity);
         //   return 1;
  
        }
        
        //更新成功---1 更新失败 --0
        public int ChangeYE(Domain.ViewModels.Charge.MeterCharge.MeterChargeModel model)//更新余额
        {
            //流水号，用户编号，记录状态，来确定数据唯一性
            int total = ChargeRecords.Count(t => t.Liu_SH == model.Liu_SH && t.Jiao_FZT == 3 && t.User_Id==model.User_ID);
            if (total != 0)
            {
                var entity = Customers.FirstOrDefault(t => t.Id == model.User_ID);
                {
                    model.Up_YE = entity.Zhang_HYE + model.Jiao_FJE;
                    entity.Zhang_HYE = model.Up_YE;
                    CustomerRepository.Update(entity);

                    var entityw = ChargeRecords.FirstOrDefault(t => t.Id == model.Id);
                    {

                        entityw.Jiao_FJE = model.Jiao_FJE;

                    };

                    ChargeRecordRepository.Update(entityw);
                    return 1;
                }
               
            }
            else
            {
                return 0;
            }


        }

        public void Update(Domain.ViewModels.Charge.MeterCharge.MeterChargeModel model)
        {
            var entity = ChargeRecords.FirstOrDefault(t => t.Liu_SH == model.Liu_SH );
            {

                entity.Jiao_FZT = model.Jiao_FZT;
                entity.ReviseDT = DateTime.Now;

                entity.Bei_Z = model.Bei_Z;
                entity.Shou_FY = model.Shou_FY;
                entity.Liu_SH = model.Liu_SH;
                model.UserName = entity.Customer.Hu_ZXM;
                model.Yong_HZZ = entity.Customer.Yong_HZZ;
                model.Ben_QDS = entity.Ben_QDS;
                model.Ben_QYL = entity.Ben_QYL;
                model.He_JYL = entity.He_JYL;
                model.User_ID = entity.Customer.Id;
                model.Ben_CJF = entity.Ben_CJF;
                model.Yong_HYE = entity.Customer.Zhang_HYE;
                
                
            };
            ChargeRecordRepository.Update(entity);
         //   return new OperationResult(OperationResultType.Success, "修改成功");
        }

        public OperationResult Update(Domain.ViewModels.Charge.ChargeRecord.ChargeRecordModel model)
        {
            var entity = ChargeRecords.FirstOrDefault(t => t.Id == model.Id);
            {

                entity.Jiao_FZT = 2;
                entity.ReviseDT = DateTime.Now;
                entity.Bei_Z = model.Bei_Z;
                entity.Shou_FY = model.Shou_FY;
                entity.Liu_SH = model.Liu_SH;

            };
            ChargeRecordRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "修改成功");
        }

    }
}


